package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.xgkj.diyiketang.activity.XiangceBigActivity;
import com.xgkj.diyiketang.data.PhotoInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.MyCustomAdapter;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.util.xiangce.BitmapCache;
import com.xgkj.diyiketang.util.xiangce.PhotoImageInfo;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/17.
 */
public class PhotoAdapter extends MyCustomAdapter {
    private Context mContext;
    private List<PhotoInfo> bmList = new ArrayList<PhotoInfo>();
    private ArrayList<PhotoImageInfo> uploadList = new ArrayList<PhotoImageInfo>();
    private BitmapCache imageLoadCache ;
    private boolean isDel;
    private showDelLayout mListener;
    private List<PhotoInfo> delList = new ArrayList<PhotoInfo>();
    BitmapCache.ImageCallback callback = new BitmapCache.ImageCallback() {
        @Override
        public void imageLoad(ImageView imageView, Bitmap bitmap,
                              Object... params) {
            if (imageView != null && bitmap != null) {
                String url = (String) params[0];
                if (url != null && url.equals((String) imageView.getTag())) {
                    ((ImageView) imageView).setImageBitmap(bitmap);
                } else {
                }
            } else {
            }
        }
    };

//    public PhotoAdapter(Context context) {
//        mContext = context;
//        imageLoadCache = new BitmapCache(mContext);
//    }


    public boolean isDel() {
        return isDel;
    }

    public void setDel(boolean del) {
        if (!del){
            getDelList().clear();
        }
        isDel = del;
    }

    public List<PhotoInfo> getDelList(){
        return delList;
    }

    public PhotoAdapter(Context context, AbsListView listView) {
        super(context, listView);
        mContext = context;
        imageLoadCache = new BitmapCache(mContext);

    }

    public PhotoAdapter(Context context, PullToRefreshGridView listview) {
        super(context, listview);
        mContext = context;
        imageLoadCache = new BitmapCache(mContext);
    }

    public void updateData(List<PhotoInfo> list){
        if(bmList != null){
            bmList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData(){
        if(bmList != null){
            bmList.clear();
        }

        if (uploadList != null){
            uploadList.clear();
        }
        notifyDataSetChanged();
    }

    public void addUploadList(ArrayList<PhotoImageInfo> list){
        PalLog.e("等待上传的图片",list.size()+"");
        uploadList = list;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {

        if(bmList != null || uploadList !=null){
            System.out.print(((int)(bmList.size()+uploadList.size()))+"");
            return (int)(bmList.size()+uploadList.size());
        }
        return 0;
    }
    @Override
    public Object getItem(int position) {
        if (uploadList != null || bmList != null){
            if (position<uploadList.size()){
                return uploadList.get(position);
            }else {
                return bmList.get(position-uploadList.size());
            }
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ChildHolder holder;
        if (null == convertView || convertView.getTag() != null) {
            holder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.photo_item,parent,false);
            holder.bdmoreImage = (ImageView) convertView.findViewById(R.id.photo_item_pic);
            holder.zhedangLayout = (RelativeLayout) convertView.findViewById(R.id.zhezhao);
            holder.mProgress = (ProgressBar) convertView.findViewById(R.id.progress);
            holder.mCheck = (CheckBox) convertView.findViewById(R.id.checkbox);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        if (position==getCount())return null;
        holder.mCheck.setVisibility(View.GONE);
        if (position<uploadList.size()){
            PhotoImageInfo uploadInfo = uploadList.get(position);
//            holder.bdmoreImage.setImageBitmap(uploadInfo.getThumbBitmap());
            imageLoadCache.displayBmp(holder.bdmoreImage,uploadInfo.getThumbnailPath(),uploadInfo.getImagePath(),callback);
            if (uploadInfo.isUploading()){
                holder.zhedangLayout.setVisibility(View.VISIBLE);
                holder.mProgress.setVisibility(View.VISIBLE);
            }else {
                holder.zhedangLayout.setVisibility(View.GONE);
                holder.mProgress.setVisibility(View.GONE);
            }
        }else {

            final PhotoInfo finfo = bmList.get(position - uploadList.size());
            if (finfo != null) {
                String foodlogo = finfo.getPhotoURL();//+"@"+ViewUtil.dip2px(mContext,88)+"w";
                if (foodlogo != null && foodlogo.length() > 0) {
                    if (!isScrolling){
                        ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(mContext,88),0);
                        Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(foodlogo,size));
                        if (cacheBitmap != null && !cacheBitmap.isRecycled()){
                            holder.bdmoreImage.setImageBitmap(cacheBitmap);
                        }else {
                            holder.bdmoreImage.setImageResource(R.drawable.zhanweitu);
                            ImageCache.getInstance(mContext).loadBitmapToView(holder.bdmoreImage, foodlogo,size);
                        }
                    }
                }
                holder.zhedangLayout.setVisibility(View.GONE);
                holder.mProgress.setVisibility(View.GONE);
                if (isDel){
                    holder.mCheck.setVisibility(View.VISIBLE);
                    if (isContains(finfo)){
                        holder.mCheck.setChecked(true);
                    }
                }
            }
            holder.mCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){
                        delList.add(finfo);
                    }else {
                        delList.remove(finfo);
                    }
                    if (mListener != null){
                        mListener.show();
                    }
                }
            });
            holder.bdmoreImage.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    setDel(true);
                    notifyDataSetChanged();
                    if (mListener != null){
                        mListener.show();
                    }
                    return false;
                }
            });

            holder.bdmoreImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent bigtIntent = new Intent(mContext,XiangceBigActivity.class);
//                        bigtIntent.putExtra("photoInfo",finfo);
                    bigtIntent.putExtra("photoInfo",(ArrayList<PhotoInfo>)bmList);
                    bigtIntent.putExtra("currentPosition",position);
                        mContext.startActivity(bigtIntent);
                }
            });
        }
        return convertView;
    }

    private boolean isContains(PhotoInfo info){
        for (int i=0;i<delList.size();i++){
            PhotoInfo selectInfo = delList.get(i);
            if (selectInfo.getPhotoURL().equalsIgnoreCase(info.getPhotoURL())){
                return true;
            }
        }
        return false;
    }


    class ChildHolder{
        ImageView bdmoreImage;
        RelativeLayout zhedangLayout;
        ProgressBar mProgress;
        CheckBox mCheck;
       // TextView bdmoreName;


    }

    public interface showDelLayout{
        public void show();
    }

    public void setDelLayoutShow(showDelLayout l){
        mListener = l;
    }
}
